package com.wolfman.wolfchat.controller;

import com.github.pagehelper.PageInfo;
import com.wolfman.wolfchat.annotation.NeedAuthentication;
import com.wolfman.wolfchat.po.Message;
import com.wolfman.wolfchat.service.MessageService;
import com.wolfman.wolfchat.vo.Result;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Title
 * @Description
 * @Author WolfMan
 * @Date 2022/1/14 23:30
 * @Email 2370032534@qq.com
 */
@RestController
@RequestMapping("/message")
public class MessageController {

    @Resource
    MessageService messageService;

    /**
     * 查询聊天记录
     * @param fromUserId
     * @param toUserId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @NeedAuthentication
    @GetMapping("/selectMessageByUserIdAndPage")
    public Result selectMessageByUserIdAndPage(@RequestParam("fromUserId") Integer fromUserId,
                                               @RequestParam("toUserId") Integer toUserId,
                                               @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                               @RequestParam(value = "pageSize", defaultValue = "15") Integer pageSize) {
        if (StringUtils.isEmpty(fromUserId) || StringUtils.isEmpty(toUserId)) {
            return Result.error("参数错误！");
        }
        PageInfo<Message> pageInfo = messageService.selectMessageByUserIdAndPage(fromUserId, toUserId, pageNum, pageSize);
        return Result.success(pageInfo);
    }

}
